package com.eastfair.config.service.entityservice.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.config.entity.ConfigAttributeValue;
import com.eastfair.config.entity.MessageTemplate;
import com.eastfair.config.service.ConfigAttributeValueService;
import com.eastfair.config.service.entityservice.MessageTemplateService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.annotation.base.LanguageTypeEnum;
import com.eastfair.enums.MsgTempEnum;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.projectcore.dto.ConfigAttributeDTO;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_ID;
import static com.eastfair.core.exception.code.ExceptionCode.ERROR_NULL_PARAMS;

/**
 * 消息模板服务
 * Date:2021/8/7
 * Time:15:45
 *
 * @author zjn
 */
@Service
public class MessageTemplateServiceImpl implements MessageTemplateService {

    @Autowired
    private ProjectCoreServiceFeign projectCoreServiceFeign;
    @Autowired
    private ConfigAttributeValueService configAttributeValueService;

    @Override
    public List<ConfigAttributeVO> getAttributes() {
        ConfigAttributeDTO configAttributeDTO = new ConfigAttributeDTO();
        configAttributeDTO.setEntityClass(MessageTemplate.class.getName());
        return projectCoreServiceFeign.listByEntityClass(configAttributeDTO).getData();
    }

    @Override
    public List<MessageTemplate> getList(MessageTemplate messageTemplate) {
        List<MessageTemplate> messageTemplateList = configAttributeValueService.listEntityByClass(MessageTemplate.class);
        if (messageTemplateList == null || messageTemplateList.isEmpty()) {
            return null;
        }
        if (Objects.isNull(messageTemplate)) {
            return messageTemplateList;
        }
        // 选择性过滤
        return messageTemplateList.stream().filter(s -> compare(s, messageTemplate)).collect(Collectors.toList());
    }

    @Override
    public R<MessageTemplate> get(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }
        MessageTemplate messageTemplate = MessageTemplate.builder().flagId(flagId).build();
        List<MessageTemplate> messageTemplateList = getList(messageTemplate);
        if (messageTemplateList != null && !messageTemplateList.isEmpty()) {
            return R.success(messageTemplateList.get(0));
        }
        return R.success(null);
    }

    @Override
    public R<MessageTemplate> getByEntity(MessageTemplate messageTemplate) {
        if (StringUtils.isBlank(messageTemplate.getType(), messageTemplate.getLanguageType())) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        MessageTemplate param = MessageTemplate.builder().type(MsgTempEnum.get(messageTemplate.getType()).name())
                .languageType(LanguageTypeEnum.get(messageTemplate.getLanguageType()).name()).build();
        if (Objects.isNull(messageTemplate.getIsEnabled())) {
            param.setIsEnabled(BusinessConstant.ENABLE_YES);
        } else {
            param.setIsEnabled(messageTemplate.getIsEnabled());
        }

        List<MessageTemplate> messageTemplateList = getList(messageTemplate);
        if (messageTemplateList != null && !messageTemplateList.isEmpty()) {
            return R.success(messageTemplateList.get(0));
        }
        return R.success(null);

    }

    @Override
    public R<Boolean> saveEntity(MessageTemplate messageTemplate) {
        if (StringUtils.isBlank(messageTemplate.getType(), messageTemplate.getContent(), messageTemplate.getLanguageType())) {
            return R.fail(ERROR_NULL_PARAMS);
        }
        if (Objects.isNull(messageTemplate.getIsEnabled())) {
            messageTemplate.setIsEnabled(BusinessConstant.ENABLE_YES);
        }

        return R.success(configAttributeValueService.saveEntity(messageTemplate));
    }

    @Override
    public R<Boolean> deleteEntity(Long flagId) {
        if (Objects.isNull(flagId)) {
            return R.fail(ERROR_NULL_ID);
        }

        UpdateWrapper<ConfigAttributeValue> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ConfigAttributeValue::getProjectId, ContextUtil.getProjectId())
                .eq(ConfigAttributeValue::getFlagId, flagId).set(ConfigAttributeValue::getIsDelete, BusinessConstant.DELETE_YES);
        return R.success(configAttributeValueService.update(updateWrapper));
    }

    @Override
    public R<Boolean> updateEntity(MessageTemplate messageTemplate) {
        if (Objects.isNull(messageTemplate.getFlagId())) {
            return R.fail(ERROR_NULL_ID);
        }
        if (StringUtils.isBlank(messageTemplate.getType()) && StringUtils.isBlank(messageTemplate.getLanguageType())
                && StringUtils.isBlank(messageTemplate.getContent()) && StringUtils.isBlank(messageTemplate.getTitle())
                && Objects.isNull(messageTemplate.getMailingId()) && Objects.isNull(messageTemplate.getIsEnabled())
        ) {
            return R.fail(ERROR_NULL_PARAMS);
        }

        return R.success(configAttributeValueService.updateEntity(messageTemplate));
    }

    /**
     * 消息模板过滤器
     *
     * @param target   目标值
     * @param standard 过滤条件
     * @return
     */
    private Boolean compare(MessageTemplate target, MessageTemplate standard) {
        if (Objects.isNull(target) || Objects.isNull(standard)) {
            return true;
        }

        if (!Objects.isNull(standard.getFlagId())) {
            if (!standard.getFlagId().equals(target.getFlagId())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getTitle())) {
            if (StringUtils.isBlank(target.getTitle()) || !target.getTitle().contains(standard.getTitle())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getType())) {
            if (!standard.getType().equals(target.getType())) {
                return false;
            }
        }
        if (!StringUtils.isBlank(standard.getLanguageType())) {
            if (!standard.getLanguageType().equals(target.getLanguageType())) {
                return false;
            }
        }
        if (!Objects.isNull(standard.getMailingId())) {
            if (!standard.getMailingId().equals(target.getMailingId())) {
                return false;
            }
        }
        if (!Objects.isNull(standard.getIsEnabled())) {
            if (!standard.getIsEnabled().equals(target.getIsEnabled())) {
                return false;
            }
        }
        return true;
    }
}